/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


/* ==========================================================================
   CONSTANTES
   ========================================================================== */
DOMINIO = window.location.protocol+"//"+window.location.hostname+"/"; 
DOMINIOADMIN =DOMINIO+"admin/";

jQuery(function(){

/* ==========================================================================
   Cria tooltips
   ========================================================================== */
    if(issetElement(".tp")){ jQuery(".tp").tooltip(); }
    
/* ==========================================================================
   Cria os switch button on/off
   ========================================================================== */
    if(issetElement(".on_off")){
        jQuery('.on_off').switchButton({ width:30, height:15 });
    }
    
/* ==========================================================================
   TELA MENU
   ========================================================================== */
    if(issetElement("#btn_del_menu")){
        jQuery("#btn_del_menu").on("click",function(e){
           var $_this = jQuery(this);
           swal({
                type: "warning",
                title:"Atenção",
                text:"Deseja mesmo apagar este menu",
                showCancelButton:true,
                confirmButtonText:"Sim, desejo",
		cancelButtonText:"Nao, foi um engano",
                confirmButtonColor:"#EC5E67",
		closeOnConfirm: false
           },function(){
                var url = DOMINIOADMIN+"menu/deletemenu",
                    id_pk_menu = $_this.data('id_pk_menu');
                post_requests(url,{id_pk_menu:id_pk_menu},function(r){
                    var $_obj = jQuery.parseJSON(r),optionsAlert;
                    if($_obj.status){
                        optionsAlert = {type:"success", title:'Sucesso', text:$_obj.msg};
                    }else{
                        optionsAlert = {type:"error", title:"Error", text:$_obj.msg}
                    }
                    swal(optionsAlert,function(){
                        window.location.href=DOMINIOADMIN+"menu";
                    });
                });
           });
        });
    }
    
    
    if(issetElement("#nestablelist")){
        
        var updateOutput = function(e){
            var list   = e.length ? e:jQuery(e.target), output = list.data('output');
            if(window.JSON){
                output.val(window.JSON.stringify(list.nestable('serialize')));
            }else{
                output.val("SEU NAVEGADOR NAO TEM SUPORTE A JSON");
            }
        }
        
        /*var updateRegister = function(e){
            console.log(e.target);
        } */
        
        /*accordion menu*/
        jQuery(document).on("click",".nestablePanel",function(){
            var $_this = jQuery(this), $_boxEdit = $_this.next();
            jQuery('.nestablePanel').find('span').removeClass('caret-up').addClass('caret');
            if(!$_boxEdit.is(":visible")){
                jQuery('.dd-edit').slideUp();
                $_this.find('span').removeClass('caret').addClass('caret-up');
                $_boxEdit.slideDown();
            }else{
                 $_this.find('span').removeClass('caret-up').addClass('caret');
                $_boxEdit.slideUp();
            }
        });
        
        jQuery("#nestablelist").nestable({
            group:1,
            maxDepth:10
        }).on("change",updateOutput);
        
        //jQuery("#nestablelist").on("change",updateRegister);
        
        updateOutput(jQuery('#nestablelist').data('output', $('#output_json')));
    }
/* ==========================================================================
   LOGIN SISTEMA
   ========================================================================== */
    if(issetElement("#logout")){
	jQuery("#logout").on("click",function(){
	    var $_this = jQuery(this);
	    swal({
		title:"Atenção",
		text:"Deseja realmente sair do sistema",
		showCancelButton:true,
		confirmButtonText:"Sim, quero sair",
		cancelButtonText:"Nao, foi um engano",
		closeOnConfirm: false
	    },function(){
		var url = DOMINIO+"admin/aplications/actions/login.act.php",
		    id_user = $_this.data('id_user'); 
		post_requests(url,{acao:"logout",id_user:id_user},function(r){
		    var $_obj = jQuery.parseJSON(r);
		    if($_obj.status){
			window.location.reload();
		    }
		});
	    });
	    return false;
	});
    }

/* ==========================================================================
   PAGINAS CONFIGURACAO
   ========================================================================== */
    //altera o modo das paginas
    if(issetElement('.model_page')){
        jQuery('.model_page').find('label').each(function(i,v){
            var $_this = jQuery(v), 
                value = $_this.children('input:radio').val(), 
                id_pagina=$_this.data('id');
            $_this.on('click',function(e){
                post_requests(DOMINIOADMIN+"paginas/alteramodo",{id_pagina:id_pagina,modelo:value},function(e){
                    
                });
            });
        });
    }
    
    //Excluir a pagina
    if(issetElement('.del_page')){
        jQuery(".del_page").each(function(){
            var $_this = jQuery(this), id_page = $_this.data('id');
            $_this.on("click",function(){
                swal({
                    type: "warning",
                    title:'Atenção',
                    text:'Deseja apagar esta pagina e todo seu conteudo, Ao confirmar esta operação nao podera ser desfeita',
                    showCancelButton:true,
                    confirmButtonText:"Confirmar",
                    cancelButtonText:"Cancelar",
                    confirmButtonColor:"#EC5E67",
                    closeOnConfirm:false,
                },function(isConfirm){
                    if(isConfirm){
                       var url = DOMINIO+"admin/paginas/delete/"+id_page, optionsAlert;
                       post_requests(url,{id:id_page},function(r){
                           var $_obj = jQuery.parseJSON(r);
                           if($_obj.status){
                               optionsAlert = {type:"success", title:'Sucesso', text:$_obj.msg};
                           }else{
                               optionsAlert = {type:"error", title:"Error", text:$_obj.msg};
                           }
                           swal(optionsAlert,function(){
                               window.location.href=DOMINIOADMIN+"paginas";
                           });
                       });
                    }
                });
            });
        });
    }
    
    
/* ==========================================================================
   PAGINA PERFIL ADMIN
   ========================================================================== */
    if(issetElement("#perfil")){
        
    }
    
});
